package com.youyun56.gps.android;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;

import com.youyun56.gps.android.commands.CommandType;
import com.youyun56.gps.android.pojo.Car;
import com.youyun56.gps.android.service.LocationUploadService;

public class CarInfoHandler extends Handler {
    private UserInfoActivity activity;
    private final static String tag = CarInfoHandler.class.getName();

    public CarInfoHandler(UserInfoActivity activity) {
        this.activity = activity;
    }

    @Override
    public void handleMessage(Message msg) {
        // TODO Auto-generated method stub
        super.handleMessage(msg);
        try {
            JSONObject carJson = (JSONObject) msg.obj;
            TextView lastPositionTime = (TextView) activity.findViewById(R.id.lastPositionTime);
            TextView carryStatus = (TextView) activity.findViewById(R.id.carryStatus);
            TextView position = (TextView) activity.findViewById(R.id.position);
            Car car = LocationUploadService.getInstance().getCar();
            car.setLastPositionTime(carJson.getString("lastPositionTime"));
            car.setCarryStatus(carJson.getString("carryStatus"));
            car.setPosition(carJson.getString("position"));
            lastPositionTime.setText(car.getLastPositionTime());
            activity.changeCarryStatus(car.getCarryStatus(), carryStatus);
            position.setText(car.getPosition());
            if (CarStatusActivity.handler != null) {
                Message me = CarStatusActivity.handler.obtainMessage();
                me.what = CommandType.Update_Car_Status_Text;
                me.sendToTarget();
            }
        } catch (JSONException e) {
            Log.e(tag, e.getMessage(), e);
        }
    }

}
